home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / TIMER.C < prev    next >
C/C++ Source or Header  |  1990-06-11  |  2KB  |  88 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "timer.h"
  4.  
  5. /* Head of running timer chain */
  6. struct timer *timers;
  7.  
  8. tick()
  9. {
  10.     register struct timer *t,*tp;
  11.     register struct timer *expired = NULLTIMER;
  12.     /*char i_state;*/
  13.  
  14.     /* Run through the list of running timers, decrementing each one.
  15.      * If one has expired, take it off the running list and put it
  16.      * on a singly linked list of expired timers
  17.      */
  18.     /*i_state = disable();*/
  19.     for(t = timers;t != NULLTIMER; t = tp){
  20.         tp = t->next;
  21.         if(tp == t){
  22.             /*restore(i_state);*/
  23.             printf("PANIC: Timer loop at %lx\n",ptr2long(tp));
  24.             iostop();
  25.             exit(1);
  26.         }
  27.         if(t->state == TIMER_RUN && --(t->count) == 0){
  28.             stop_timer(t);
  29.             t->state = TIMER_EXPIRE;
  30.             /* Put on head of expired timer list */
  31.             t->next = expired;
  32.             expired = t;
  33.         }
  34.     }
  35.     /*restore(i_state);*/
  36.     /* Now go through the list of expired timers, removing each
  37.      * one and kicking the notify function, if there is one
  38.      */
  39.     while((t = expired) != NULLTIMER){
  40.         expired = t->next;
  41.         if(t->func){
  42.             (*t->func)(t->arg);
  43.         }
  44.     }
  45. }
  46. /* Start a timer */
  47. start_timer(t)
  48. register struct timer *t;
  49. {
  50.     /*char i_state;*/
  51.  
  52.     if(t == NULLTIMER || t->start == 0)
  53.         return;
  54.     /*i_state = disable();*/
  55.     t->count = t->start;
  56.     if(t->state != TIMER_RUN){
  57.         t->state = TIMER_RUN;
  58.         /* Put on head of active timer list */
  59.         t->prev = NULLTIMER;
  60.         t->next = timers;
  61.         if(t->next != NULLTIMER)
  62.             t->next->prev = t;
  63.         timers = t;
  64.     }
  65.     /*restore(i_state);*/
  66. }
  67. /* Stop a timer */
  68. stop_timer(t)
  69. register struct timer *t;
  70. {
  71.     /*char i_state;*/
  72.  
  73.     if(t == NULLTIMER)
  74.         return;
  75.     /*i_state = disable();*/
  76.     if(t->state == TIMER_RUN){
  77.         /* Delete from active timer list */
  78.         if(timers == t)
  79.             timers = t->next;
  80.         if(t->next != NULLTIMER)
  81.             t->next->prev = t->prev;
  82.         if(t->prev != NULLTIMER)
  83.             t->prev->next = t->next;
  84.     }
  85.     t->state = TIMER_STOP;
  86.     /*restore(i_state);*/
  87. }
  88.